<!--轮播图-->
<template>
    <div class="video-category flex-center-container">
        <button @click="prevSlide()" 
        class="video-category-controller controller-left common-based-btn font-fifth-color">上一项</button>
        <div class="video-category-item based-box common-based-btn" 
        :class="item.id-1==currentIndex?'tags-and-labels':'recommend-labels'" 
        v-for="(item, index) in config.recommendCategory" :key="index"
        @click="changeRecommendType(item.id-1)"><!--NOTE 虽然@v-for不报错，但不能这么用。因为v-for是指令而非事件-->
            {{item.name}}
        </div>
        <button @click="nextSlide()" 
        class="video-category-controller controller-right common-based-btn font-fifth-color">下一项</button>
    </div>
</template>

<script setup>
import { computed, defineProps, defineEmits, ref } from "vue"
/**
 * props和emits
 */
const config = defineProps({
    currentIndex: {
        type: Number,
        required: true,
        default: 0
    },
    itemsPerPage: {
        type: Number,
        required: true,
        default: 0
    },
    recommendCategory: {
        type: Array,
        required: true,
        default: undefined
    }    
})
const recLen = ref(config.recommendCategory.length)
const currentIndex = ref(config.currentIndex)
const emits = defineEmits('editIndex')
/**
 * 可视化分类
 */
const visibleCategory = computed(() => {
    // 根据当前索引和每页显示数量，计算轮播项
    const start =currentIndex.value
    const end = start + config.itemsPerPage;
    let visibleData = config.recommendCategory.slice(start, end)
    // 如果start过大
    let i = 0
    while(visibleData.length < config.itemsPerPage) {
        visibleData.push(config.recommendCategory[i++])
    } 
    return visibleData
})
/**
 * 推荐相关的交互
 */
// 切换推荐状态
const changeRecommendType = (index) => {
    currentIndex.value = index
    emits('editIndex', currentIndex.value)
}
// 推荐类型的滚动
const prevSlide = () => {
    currentIndex.value = (currentIndex.value - 1 + recLen.value) % recLen.value
    emits('editIndex', currentIndex.value)
}
const nextSlide = () => {
    currentIndex.value = (currentIndex.value + 1) % recLen.value
    emits('editIndex', currentIndex.value)
}
</script>

<style lang="scss" scoped>
.video-category {
    width: 35rem;
    height: 3rem;
    margin: 1.5rem 1rem;
    z-index: 1;
}

.video-category-item {
    padding: 0.6rem;
    width: auto;
    max-width: 5rem;
    height: 1.3rem;
    margin-left: 0.2rem;
    margin-right: 0.2rem;
}

.recommend-labels {
    color: #052b54a9;
}

.video-category .video-category-controller {
    padding: 0.3rem;
    width: 4rem;
    height: 2.5rem;
    border: 1px solid #9ac9fb;
    background-color: #fff;
    position: absolute;
}

.controller-left {
    left: 1rem;
}

.controller-right {
    right: -1rem;
}
</style>